subroutine record_xsec(nlog,nout)
  use param
  use global
  implicit none
  integer,intent(in) :: nlog,nout
  integer :: i,j,sp,td
  real(dbl) :: e,r,h
  integer :: pos,atd
  logical :: foo
  xmine=2.0*sube
  if (xmode.eq.2) then
     ! ------------------------------------------- !
     if (xmaxe.lt.0) stop
     if (xeh.lt.0) stop
     if (xih.lt.0) stop
     if (xeh.lt.xih) xih=0.2*xeh
     ! ------------------------------------------- !
     ! xmine
     td=1
     ! xgrid
     td=td+int((xmaxe-xmine)/xeh)
     ! xmaxe
     if (mod(xmaxe-xmine,xeh).ne.0) td=td+1
     r=real(td)
     ! ------------------------------------------- !
     open(unit=nout,file=outfn,access='direct',status='replace',recl=8)
     ! -- file info
     pos=0
     atd=0
     call dwrite(nout,pos,xmine)
     call dwrite(nout,pos,xmaxe)
     call dwrite(nout,pos,r)
     write(nlog,'(3x,"Minimum energy(2*a)=",1pe9.3)') xmine
     write(nlog,'(3x,"Maximum energy=",1pe9.3)') xmaxe
     write(nlog,'(3x,"Number of grids=",i0)') td
     write(*,'(3x,"Minimum energy(2*a)=",1pe9.3)') xmine
     write(*,'(3x,"Maximum energy=",1pe9.3)') xmaxe
     write(*,'(3x,"Number of grids=",i0)') td
     ! -- initial
     e=xmine
     r=0.0
     call dwrite(nout,pos,e)
     call dwrite(nout,pos,r)
     atd=atd+1
     write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     ! -- middle
     do i=2,td-1
        e=e+xeh
        call get_xsec(e,sube,xih,r)
        call dwrite(nout,pos,e)
        call dwrite(nout,pos,r)
        atd=atd+1
        write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
        write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     end do
     ! -- final
     e=xmaxe
     call get_xsec(e,sube,xih,r)
     call dwrite(nout,pos,e)
     call dwrite(nout,pos,r)     
     atd=atd+1
     write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     write(nlog,'(3x,"Total number of data : Recorded=",i0,3x,"Counted=",i0 )') td,atd
     write(*,'(3x,"Total number of data : Recorded=",i0,3x,"Counted=",i0 )') td,atd
     close(nout)
  else if (xmode.eq.1) then
     ! ------------------------------------------- !
     ! Set min,max
     xmaxe=egrid(n_egrid)
     ! xmine
     td=1
     ! tgrid
     foo=.true.
     do i=1,n_rspec
        if (egrid(i).gt.xmine) then
           foo=.false.
           sp=i
           exit
        end if
     end do
     if (foo) stop
     if (sp.eq.1) then
        td=td+nhx*(n_rspec+1)
     else
        td=td+nhx*(n_rspec-sp+2)
     end if
     r=real(td)
     ! ------------------------------------------- !
     open(unit=nout,file=outfn,access='direct',status='replace',recl=8)
     ! -- file info
     pos=0
     atd=0
     call dwrite(nout,pos,xmine)
     call dwrite(nout,pos,xmaxe)
     call dwrite(nout,pos,r)
     write(nlog,'(3x,"Minimum energy=",1pe9.3)') xmine
     write(nlog,'(3x,"Maximum energy=",1pe9.3)') xmaxe
     write(nlog,'(3x,"Number of grids=",i0)') td
     write(*,'(3x,"Minimum energy=",1pe9.3)') xmine
     write(*,'(3x,"Maximum energy=",1pe9.3)') xmaxe
     write(*,'(3x,"Number of grids=",i0)') td
     ! -- initial
     e=xmine
     r=0.0
     call dwrite(nout,pos,e)
     call dwrite(nout,pos,r)
     write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
     atd=atd+1
     if (sp.eq.1) then
        h=(egrid(1)-xmine)/real(nhx)
        do i=1,nhx
           e=e+h
           xih=(e-sube)/real(nix)
           call get_xsec(e,sube,xih,r)
           call dwrite(nout,pos,e)
           call dwrite(nout,pos,r)
           write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
           write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
           atd=atd+1
        end do
        do i=1,n_rspec
           h=(egrid(i+1)-egrid(i))/real(nhx)
           e=egrid(i)
           do j=1,nhx
              e=e+h
              xih=(e-sube)/real(nix)
              call get_xsec(e,sube,xih,r)
              call dwrite(nout,pos,e)
              call dwrite(nout,pos,r)
              write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
              write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
              atd=atd+1
           end do
        end do
     else
        h=(egrid(sp)-xmine)/real(nhx)
        do i=1,nhx
           e=e+h
           xih=(e-sube)/real(nix)
           call get_xsec(e,sube,xih,r)
           call dwrite(nout,pos,e)
           call dwrite(nout,pos,r)
           write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
           write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
           atd=atd+1
        end do
        do i=sp,n_rspec
           h=(egrid(i+1)-egrid(i))/real(nhx)
           e=egrid(i)
           do j=1,nhx
              e=e+h
              xih=(e-sube)/real(nix)
              call get_xsec(e,sube,xih,r)
              call dwrite(nout,pos,e)
              call dwrite(nout,pos,r)
              write(nlog,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
              write(*,'(3x,"E=",1pe9.3,1x,"Xsec=",1pe9.3)') e,r
              atd=atd+1
           end do
        end do
     end if
     write(nlog,'(3x,"Total number of data : Recorded=",i0,3x,"Counted=",i0 )') td,atd
     write(*,'(3x,"Total number of data : Recorded=",i0,3x,"Counted=",i0 )') td,atd
     close(nout)
  else
     stop
  end if
end subroutine record_xsec
